<?php
abstract class LBController extends Zend_Controller_Action
{
    protected $_user;

    const AJAX_RESPONSE_SUCC = 0;
    const AJAX_RESPONSE_FAIL = -1;

    public function init()
    {/*{{{*/
        //prepare view
        $this->initView();
        $this->view->setBasePath(dirname(dirname(__FILE__)).'/views/');

        //get config
        $config = Zend_Registry::getInstance()->get('config');

        $this->assign('mediaUrl', $config->get('mediaurl'));
        $this->assign('baseUrl', $this->_request->getBaseUrl());
        $this->assign('isSelf', $this->isSelf());
        $this->assign('title', gettext('lb_title'));

        $user = $this->getUserInfo();
        $this->assign('user', $user);

        $this->needTimer = $this->needTimer();
    }/*}}}*/

    public function noRouteAction()
    {/*{{{*/
        $this->redirect('/');
    }/*}}}*/
    
    public function indexAction()
    {/*{{{*/
        $this->redirect('/');
    }/*}}}*/

    public function preDispatch()
    {/*{{{*/
        if ($this->needTimer)
        {
            $registry = Zend_Registry::getInstance();
            $registry->get('timer')->start();
        }
        if ($this->needAuth())
        {
            $user = $this->getUserInfo();
            $this->assign('crumb', $this->getCrumb());

            if (null == $user)
            {
                //todo:
                $desturl = '';
                $this->_redirect($this->view->baseUrl.'/auth/login?desturl='.$desturl);
            }
        }
    }/*}}}*/
    
    public function postDispatch()
    {/*{{{*/
        if ($this->needTimer)
        {
            $registry = Zend_Registry::getInstance();
            $timer = $registry->get('timer');
            $timer->stop();
            echo '<!--'.$timer->elapsed().'//-->';
            var_dump($timer->elapsed());
        }
    }/*}}}*/

    protected function needTimer()
    {
        return true;
    }

    protected function needAuth()
    {
        return true;
    }

    protected function getParam($key, $default = null)
    {/*{{{*/
        return $this->_getParam($key, $default);
    }/*}}}*/

    protected function getSessSvc()
    {/*{{{*/
        $registry = Zend_Registry::getInstance();
        return $registry->get('sessvc');
    }/*}}}*/

    protected function getUserInfo()
    {/*{{{*/
        static $res = array();
        if (empty($res))
        {
            $sess = $this->getSessSvc();
            $res = $sess->get('info');
        }
        return $res;
    }/*}}}*/

    protected function getUserId($userId=0)
    {/*{{{*/
        if (0 === (int)$userId)
        {
            $userInfo = $this->getUserInfo();
            $userId = $userInfo['userId'];
        }
        return $userId;
    }/*}}}*/

    protected function isSelf()
    {
        $u = $this->getParam('u', 0);
        if (0 < (int)$u)
        {
            return $u == $this->getUserId();
        }
        return true;
    }

    protected function getUserName()
    {/*{{{*/
        $userInfo = $this->getUserInfo();
        return $userInfo['userName'];
    }/*}}}*/

    protected function getCrumb()
    {/*{{{*/
        $userInfo = $this->getUserInfo();
        return $userInfo['crumb'];
    }/*}}}*/


    protected function redirect($desturl, $options = array())
    {/*{{{*/
        if ( (null == $desturl) || (false == filter_var($desturl, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) )
        {
            $this->_redirect('/', $options);
            /*
             * todo: need to skip the login, register 
            $referer = getenv('HTTP_REFERER');
            if (false == $referer)
            {
                $this->_redirect('/', $options);
            }
            else
            {
                $this->_redirect($referer, $options);
            }*/
        }
        else
        {
            $this->_redirect($desturl, $options);
        }
    }/*}}}*/

    protected function baseContent()
    {
        $userId = filter_var($this->getParam('u', 0), FILTER_VALIDATE_INT);
        $userId = $this->getUserId($userId);

        $prf = new Profile();
        $profile = $prf->findByUserId($userId);

        if (null == $profile)
        {
            throw new UserException(UserException::USER_NOT_EXIST);
        }

        $frd = new Friend();
        $friends = $frd->getFriends($userId);
        $this->assign('friends', $friends);
        $this->assign('profile', $profile->toArray());

        $evt = new Event();
        $events = $evt->findByAttender($userId);
        $this->assign('events', $events);
    }

    protected function assign($key, $value)
    {
        $this->view->{$key} = $value;
    }

    protected function renderAjax($res, $encode=true, $tpl='response')
    {
        $this->assign('encode', $encode);
        $this->assign('msg', $res);
        return $this->render('ajax/'.$tpl, null, true);
    }
}
